package netty01;


import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class HelloServer {

    public void run() throws InterruptedException {

        //设置线程组
        //主线程，类似boss接收连接，但是不处理
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        //从线程，boss线程接到任务让worker去处理
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new HelloServerInitializer());

            //侦听端口，同步方式一直等待
            ChannelFuture channelFuture = serverBootstrap.bind(8088).sync();
            //关闭channel
            channelFuture.channel().closeFuture().sync();
        } finally {
            //关闭线程组
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }


    }

    public static void main(String[] args) throws InterruptedException {
        new HelloServer().run();
    }
}
